package com.zeus.mongo.mapper;

import com.zeus.mongo.helper.MongoHelper;
import org.bson.Document;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoExampleMapper;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author magic
 * @date 19-7-18 上午10:49
 * @version 1.0.0
 * Description ZeusExampleMapper
 *
 */
public class ZeusExampleMapper extends MongoExampleMapper {

    public ZeusExampleMapper(MongoConverter converter) {
        super(converter);
    }

    @Override
    public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) {
        Document document = super.getMappedExample(example, entity);
        List<Criteria> criteria = MongoHelper.get();
        criteria.forEach(c ->{
            Set<Map.Entry<String, Object>> entrySet = c.getCriteriaObject().entrySet();
            Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> objectEntry = iterator.next();
                String key = objectEntry.getKey();
                Object value = objectEntry.getValue();
                document.put(key,value);
            }
        });
        return document;
    }
}
